<?php
declare (strict_types = 1);

namespace app\adminapi\controller\system;

use adminmatrix\admin\BaseController;
use adminmatrix\admin\service\JsonService;
use app\adminapi\logic\system\AdminRoleLogic;
use think\Request;
use think\response\Json;
use hg\apidoc\annotation as Apidoc;

#[
    Apidoc\Title("角色管理"),
    Apidoc\Group("system")
]
class RoleController extends BaseController
{
    #[
        Apidoc\Title("角色列表")
    ]
    public function lists() : Json
    {
        return  $this->dataLists();
    }

    #[
        Apidoc\Title("编辑角色"),
        Apidoc\Method("PUT")
    ]
    public function update(Request $request , AdminRoleLogic $AdminLogic) :Json
    {
        try {
            $AdminLogic->update($request);
            return JsonService::success('更新角色成功');
        } catch (\Exception $e)
        {
            return JsonService::fail($e->getMessage());
        }
    }

    #[
        Apidoc\Title("新增角色"),
        Apidoc\Method("POST")
    ]
    public function save(Request $request , AdminRoleLogic $AdminLogic) : Json
    {
        try {
            $AdminLogic->save($request);
            return JsonService::success('新增角色成功');
        } catch (\Exception $exception)
        {
            return  JsonService::fail($exception->getMessage());
        }
    }

    #[
        Apidoc\Title("删除角色"),
        Apidoc\Method("DELETE")
    ]
    public function delete(Request $request , AdminRoleLogic $adminRoleLogic) : Json
    {
        try {
            $adminRoleLogic->delete($request);
            return JsonService::success('删除角色成功');
        } catch (\Exception $exception)
        {
            return  JsonService::fail($exception->getMessage());
        }
    }

    public function setAuth(Request $request  , AdminRoleLogic $role) : Json
    {
        try {
            $role->setAuth($request);
            return  JsonService::success("更新角色权限成功");
        } catch (\Exception $e)
        {
            return JsonService::fail($e->getMessage());
        }
    }
}
